package com.chinaedu.oc.serv.commondity.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.chinaedu.oc.constant.Constant;
import com.chinaedu.oc.dao.auditrecord.IAuditrecordDao;
import com.chinaedu.oc.dao.common.IGradeDao;
import com.chinaedu.oc.dao.common.ISpecialtyDao;
import com.chinaedu.oc.dao.common.SystemDao;
import com.chinaedu.oc.dao.commondity.ICommodityAreaDao;
import com.chinaedu.oc.dao.commondity.ICommodityDao;
import com.chinaedu.oc.dao.commondity.ICommodityEducationalDao;
import com.chinaedu.oc.dao.commondity.ICommodityGradeDao;
import com.chinaedu.oc.dao.commondity.ICommodityLevelDao;
import com.chinaedu.oc.dao.commondity.ICommodityProductDao;
import com.chinaedu.oc.dao.commondity.ICommodityProductLineDao;
import com.chinaedu.oc.dao.commondity.ICommodityRelationshipDao;
import com.chinaedu.oc.dao.commondity.ICommoditySaledepartmentDao;
import com.chinaedu.oc.dao.commondity.ICommoditySpecialtyDao;
import com.chinaedu.oc.dao.commondity.ICommodityUserTypeDao;
import com.chinaedu.oc.dao.product.ProductDao;
import com.chinaedu.oc.dao.productline.ProductLineDao;
import com.chinaedu.oc.dao.saledate.ISaledateDao;
import com.chinaedu.oc.po.auditrecord.Auditrecord;
import com.chinaedu.oc.po.commodity.Commodity;
import com.chinaedu.oc.po.commodity.CommodityArea;
import com.chinaedu.oc.po.commodity.CommodityEducational;
import com.chinaedu.oc.po.commodity.CommodityGrade;
import com.chinaedu.oc.po.commodity.CommodityLevel;
import com.chinaedu.oc.po.commodity.CommodityProduct;
import com.chinaedu.oc.po.commodity.CommodityProductLine;
import com.chinaedu.oc.po.commodity.CommodityRelationship;
import com.chinaedu.oc.po.commodity.CommoditySaledepartment;
import com.chinaedu.oc.po.commodity.CommoditySpecialty;
import com.chinaedu.oc.po.commodity.CommodityUserType;
import com.chinaedu.oc.po.commodity.dto.QueryCondition;
import com.chinaedu.oc.po.product.Product;
import com.chinaedu.oc.po.productline.ProductLine;
import com.chinaedu.oc.po.saledate.Saledate;
import com.chinaedu.oc.serv.commondity.ICommodityServ;
import com.chinaedu.oc.serv.product.ICodeCreate;
import com.chinaedu.oc.util.common.IdCreater;

/**
 *
 * @author Administrator
 * 商品模块实现类
 *
 */
@Service
@Transactional
public class CommodityServImpl implements ICommodityServ {

	@Autowired
	private ICommodityDao commondityDao;

	@Autowired
	private ICommodityProductDao commodityProductDao;

	@Autowired
	private ICommoditySaledepartmentDao commoditySaledepartmentDao;

	@Autowired
	private ICommodityAreaDao iCommodityAreaDao;

	@Autowired
	private ICommodityLevelDao commodityLevelDao;

	@Autowired
	private ICommoditySpecialtyDao commoditySpecialtyDao;

	@Autowired
	private ICommodityGradeDao commodityGradeDao;

	@Autowired
	private ICommodityUserTypeDao commodityUserTypeDao;

	@Autowired
	private ICommodityEducationalDao commodityEducationalDao;

	@Autowired
	private ICommodityRelationshipDao commodityRelationshipDao;

	@Autowired
	private IAuditrecordDao auditrecordDao;

	@Autowired
	private IGradeDao iGradeDao;

	@Autowired
	private ISpecialtyDao iSpecialtyDao;

	@Autowired
	private ICommodityProductLineDao iCommodityProductLineDao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private ProductLineDao productLineDao;

	@Autowired
	private SystemDao systemDao;

	@Autowired
	private ICodeCreate iCodeCreate;

	@Autowired
	private ISaledateDao iSaledateDao;


	@Override
	public List<Commodity> queryAll(QueryCondition queryCondition) {
		// TODO Auto-generated method stub
		List<String> ids = null; // 学制对应的商品ID
		Set<String> ids1 = null;
		if (!StringUtils.isEmpty(queryCondition.getEducationalId())) {
			ids1 = commodityEducationalDao.queryCommodityIdById(queryCondition.getEducationalId());
			if(ids == null) {
				ids = new ArrayList<String>();
				for (String string : ids1) {
					ids.add(string);
				}
			}
		}
		Set<String> ids2 = null; // 部门对应的商品ID
		if (!StringUtils.isEmpty(queryCondition.getSaleDepartmentId())) {
			ids2 = commoditySaledepartmentDao.queryCommodityIdById(queryCondition.getSaleDepartmentId());
			if(ids == null) {
				ids = new ArrayList<String>();
				for (String string : ids2) {
					ids.add(string);
				}
			}else {
				ids.retainAll(ids2);
			}
			System.out.println(ids.size());
		}
		Set<String> ids3 = null;// 年级对应的商品ID
		if (!StringUtils.isEmpty(queryCondition.getGradeId())) {
			ids3 = commodityGradeDao.queryCommodityIdById(queryCondition.getGradeId());
			if(ids == null) {
				ids = new ArrayList<String>();
				for (String string : ids3) {
					ids.add(string);
				}
			}else {
				ids.retainAll(ids3);
			}
		}
		Set<String> ids4 = null;// 产品线对应的商品的ID
		if (!StringUtils.isEmpty(queryCondition.getProductLineId())) {
			ids4 = iCommodityProductLineDao.queryCommodityIdById(queryCondition.getProductLineId());
			if(ids == null) {
				ids = new ArrayList<String>();
				for (String string : ids4) {
					ids.add(string);
				}
			}else {
				ids.retainAll(ids4);
			}
		}
		Set<String> ids5 = null;// 学科对应的商品ID
		if (!StringUtils.isEmpty(queryCondition.getSpecialtyId())) {
			ids5 = commoditySpecialtyDao.queryCommodityIdById(queryCondition.getSpecialtyId());
			if(ids == null) {
				ids = new ArrayList<String>();
				for (String string : ids5) {
					ids.add(string);
				}
			}else {
				ids.retainAll(ids5);
			}
		}
		Set<String> ids6 = null;// 学段对应的商品ID
		if (!StringUtils.isEmpty(queryCondition.getStudyLevelId())) {
			ids6 = commodityLevelDao.queryCommodityIdById(queryCondition.getStudyLevelId());
			if(ids == null) {
				ids = new ArrayList<String>();
				for (String string : ids6) {
					ids.add(string);
				}
			}else {
				ids.retainAll(ids6);
			}
		}
		List<Commodity> queryByPage = null;
		if(ids != null) {
			if(ids.size() == 0) {
				return null;
			}else {
				queryCondition.setIds(ids);
			}
		}
		queryByPage = commondityDao.queryByPage(queryCondition);
		return queryByPage;
	}

	@Override
	public List<Commodity> queryByCase(Commodity commodity) {
		// TODO Auto-generated method stub
		List<Commodity> commondities = commondityDao.queryByCase(commodity);
		return commondities;
	}

	@Override
	public List<Commodity> queryByIds(List<String> ids) {
		// TODO Auto-generated method stub
		Commodity commodity = new Commodity();
		commodity.setIds(ids);
		List<Commodity> commondities = commondityDao.queryByCase(commodity);
		return commondities;
	}

	/**
	 * 说明 : 要删除商品，同时也要删除和它有关系的所有东西 如果单品被打包之后则不能被删除
	 */
	@Override
	@Transactional(propagation = Propagation.NESTED) // 勿修改
	public void delete(List<String> ids) {
		// TODO Auto-generated method stub
		boolean flag = true;
		List<String> idsRemove = new ArrayList<String>();
		for (String id : ids) {// 判断是单品还是组合品
			flag = true;// 默认可删除
			Commodity commodity = new Commodity();
			commodity.setId(id);
			List<Commodity> queryByCase = commondityDao.queryByCase(commodity);
			if(queryByCase.size() == 0) {
				continue;
			}
			System.out.println(queryByCase.get(0).getIsSingleProduct());
			if (Constant.COMMODITY_IS_SINGLEPRODUCT_A.equals(queryByCase.get(0).getIsSingleProduct())) {// 判断该单品是否被打包过
				List<CommodityRelationship> queryByChildId = commodityRelationshipDao.queryByChildId(id);
				if (queryByChildId.size() > 0) {
					flag = false;// 不做删除 打包过
					idsRemove.add(id); // 先放入集合中， 从待删除集合中移除
				} else {
					commodityProductDao.delete(id);// 删除单品和产品的对应关系
				}
			} else {
				commodityRelationshipDao.delete(id);// 组合品删除商品和商品之间的关系
			}
			if (flag) {// 删除商品和学段 学制 年级 学科 用户类型 销售部门 地区的对应关系
				commodityGradeDao.delete(id);
				commodityEducationalDao.delete(id);
				commodityLevelDao.delete(id);
				commoditySaledepartmentDao.delete(id);
				commodityUserTypeDao.delete(id);
				commoditySpecialtyDao.delete(id);
				iCommodityProductLineDao.delete(id);
				iCommodityAreaDao.delete(id);
			}
		}
		for (String id : idsRemove) {
			ids.remove(id);
		}
		if (ids.size() > 0) {// 批量删除商品
			commondityDao.delete(ids);
		}
	}

	@Override
	public void updateById(Commodity commodity) {
		// TODO Auto-generated method stub
		List<Auditrecord> auditrecords = auditrecordDao.findInfoByCommodityId(commodity.getId());
		Auditrecord auditrecord = commodity.getAuditrecord();
		auditrecord.setAuditDate(new Timestamp(System.currentTimeMillis()));
		if(auditrecords.size() > 0) {
			auditrecord.setSponsor(auditrecords.get(0).getSponsor());
		}
		auditrecord.setId(IdCreater.getId());
		auditrecord.setCommodityId(commodity.getId());
		auditrecord.setAuditStatus(commodity.getAuditStatus());
		auditrecord.setAuditDate(new Timestamp(System.currentTimeMillis()));
		if (Constant.COMMODITY_REVIEW_STATUS_C.equals(commodity.getAuditStatus())) {
			auditrecord.setAuditOpinion("一审通过");
		} else if (Constant.COMMODITY_REVIEW_STATUS_D.equals(commodity.getAuditStatus())) {
			auditrecord.setAuditOpinion("二审通过");
		} else if (Constant.COMMODITY_REVIEW_STATUS_B.equals(commodity.getAuditStatus())) {
			auditrecord.setAuditOpinion("未审核");
		} else {// 驳回
		}
		commondityDao.updateByCommodity(commodity);
		auditrecordDao.insert(auditrecord);

	}

	/**
	 * 新增或者编辑商品时调用此方法 涉及到事物的传播
	 */
	@Override
	public void insertOrUpdate(Commodity commodity) {
		// TODO Auto-generated method stub
		Boolean isInsert = true;   //判断是增加或则是修改
		if (StringUtils.isEmpty(commodity.getId())) {  //组合商品不能为赠品 如果商品的id为空则设置ID
			commodity.setId(IdCreater.getId());
			commodity.setIsPrice(Constant.COMMODITY_IS_PRICE_A);
			commodity.setIsGive(Constant.COMMODITY_IS_GIVE_B);
			commodity.setIsHasSaledate(Constant.COMMODITY_IS_HAS_SALEDATE_B); // 是否有销售期
			commodity.setIsSingleProduct(Constant.COMMODITY_IS_SINGLEPRODUCT_B); // 默认组合品
			commodity.setIsGrounding(Constant.COMMODITY_IS_GROUP_A); // 默认未上架
			commodity.setCreateDate(new Timestamp(System.currentTimeMillis()));  //创建日期
			String commodityCode = null;
			List<CommodityRelationship> commodityRelationships = commodity.getCommodityRelationship();//生成编码
			Set<String> set = new HashSet<String>();
			if(commodityRelationships != null) {
				String productLineId = null;
				for (CommodityRelationship commodityRelationship : commodityRelationships) {
					String childId = commodityRelationship.getChildId();
					List<CommodityProductLine> findByCommodityId = iCommodityProductLineDao.findByCommodityId(childId); //单品只有一个产品线
					if(findByCommodityId.size() > 0) {
						set.add(findByCommodityId.get(0).getProductLineNameId());
						productLineId = findByCommodityId.get(0).getProductLineNameId();
					}
				}
				if(set.size() == 1) {//同一产品线
					ProductLine productLine = productLineDao.selectProductLineById(productLineId);
					if(productLine != null) {//生成商品code
						int zh = iCodeCreate.createCode("zh");
						int sp = iCodeCreate.createCode("sp");
						commodityCode = productLine.getCode()+"ZH"+zh+"sp"+sp;
					}
				}else {//不同产品线
					int zh = iCodeCreate.createCode("zh");
					int sp = iCodeCreate.createCode("sp");
					int pl = iCodeCreate.createCode("pl");
					commodityCode = "D"+pl+"ZH"+zh+"SP"+sp;//生成商品code
				}
			}
			commodity.setCommodityCode(commodityCode);
		} else {
			commodity.setIsGive(Constant.COMMODITY_IS_GIVE_B);
			commodity.setModifyDate(new Timestamp(System.currentTimeMillis()));
			isInsert = false;
			deleteById(commodity.getId()); // 先删除原来的商品 然后保存
		}
		if (commodity.getIsSubmitAudit() == Constant.COMMODITY_IS_SUBMITAUDIT_A
				|| commodity.getIsSubmitAudit() == null) {
			commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_A);
			commodity.setIsEdited(Constant.COMMODITY_IS_EDIT_A); // 未提交审核 可编辑
		} else {// 能提交审核 并且已提交审核但是还未经过一审 此时需要将数据保存到审核历史表中
			Auditrecord auditrecord = new Auditrecord();
			auditrecord.setId(IdCreater.getId());
			auditrecord.setAuditDate(new Timestamp(System.currentTimeMillis()));
			if(StringUtils.isEmpty(commodity.getId())) {
				auditrecord.setSponsor(commodity.getCreatorId());
			}else {
				auditrecord.setSponsor(commodity.getModifyUser());
			}
			if (commodity.getDiscount() != 1) {// 有折扣 优惠 现价和原价不等需要审核
				commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_B);
				auditrecord.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_B);
			} else {// 没折扣相等 直接审核通过
				commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_D);
				commodity.setIsGrounding(Constant.COMMODITY_IS_GROUP_B);
				auditrecord.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_D);
				// auditrecord.setCommodityId(null); //如果为空 则代表自动审核通过
			}
			auditrecord.setCommodityId(commodity.getId());
			commodity.setIsEdited(Constant.COMMODITY_IS_EDIT_B); // 提交审核后    商品不可编辑状态
			auditrecordDao.insert(auditrecord);
		}
		if (commodity.getCommoditySaledepartments() != null) {// 如果销售部门的ID为空则设置ID
			for (int i = 0; i < commodity.getCommoditySaledepartments().size(); i++) {
				if (StringUtils.isEmpty(commodity.getCommoditySaledepartments().get(i).getId())) {
					commodity.getCommoditySaledepartments().get(i).setId(IdCreater.getId());
					commodity.getCommoditySaledepartments().get(i).setCommodityId(commodity.getId());
				}
			}
			if(commodity.getCommoditySaledepartments().size() > 0) {// 增加和销售部门的关系
				commoditySaledepartmentDao.insert(commodity.getCommoditySaledepartments());
			}
		}
		if (commodity.getCommodityLevels() != null) {// 如果学段的ID为空则设置ID
			for (int i = 0; i < commodity.getCommodityLevels().size(); i++) {
				if (StringUtils.isEmpty(commodity.getCommodityLevels().get(i).getId())) {
					commodity.getCommodityLevels().get(i).setId(IdCreater.getId());
					commodity.getCommodityLevels().get(i).setCommodityId(commodity.getId());
				}
			}
			if(commodity.getCommodityLevels().size() > 0) {// 添加和学段的关系
				commodityLevelDao.insert(commodity.getCommodityLevels());
			}
		}
		if (commodity.getCommoditySpecialty() != null) {// 如果学科的ID为空则设置ID
			List<String> ids = new ArrayList<String>();
			for (int i = 0; i < commodity.getCommoditySpecialty().size(); i++) {
				if (StringUtils.isEmpty(commodity.getCommoditySpecialty().get(i).getId())) {
					commodity.getCommoditySpecialty().get(i).setId(IdCreater.getId());
					commodity.getCommoditySpecialty().get(i).setCommodityId(commodity.getId());
				}
				ids.add(commodity.getCommoditySpecialty().get(i).getSpecialtyId());
			}
			String specialtyNames = iSpecialtyDao.findSpecialtyByIds(ids);// 学科名
			if (specialtyNames != null) {
				specialtyNames.replaceAll(",", " ");
			}
			commodity.setSpecialtyName(specialtyNames);
			if( commodity.getCommoditySpecialty().size() > 0) {
				commoditySpecialtyDao.insert(commodity.getCommoditySpecialty());// 添加和学科的关系
			}
		}
		if (commodity.getCommodityGrade() != null) {// 如果年级的ID为空则设置ID
			List<String> ids = new ArrayList<String>();
			for (int i = 0; i < commodity.getCommodityGrade().size(); i++) {
				if (StringUtils.isEmpty(commodity.getCommodityGrade().get(i).getId())) {
					commodity.getCommodityGrade().get(i).setId(IdCreater.getId());
					commodity.getCommodityGrade().get(i).setCommodityId(commodity.getId());
				}
				ids.add(commodity.getCommodityGrade().get(i).getGradeId());
			}
			String gradeNames = iGradeDao.findGradeByIds(ids);// 年级名
			if (gradeNames != null) {
				gradeNames.replaceAll(",", " ");
			}
			commodity.setGradeName(gradeNames);
			if(commodity.getCommodityGrade().size() > 0) {// 添加和年级的关系
				commodityGradeDao.insert(commodity.getCommodityGrade());
			}
		}
		if (commodity.getCommodityAreas() != null) {// 保存和地区的关系
			for (int i = 0; i < commodity.getCommodityAreas().size(); i++) {
				if (StringUtils.isEmpty(commodity.getCommodityAreas().get(i).getId())) {
					commodity.getCommodityAreas().get(i).setId(IdCreater.getId());
					commodity.getCommodityAreas().get(i).setCommodityId(commodity.getId());
				}
			}
			if(commodity.getCommodityAreas().size() > 0) {// 添加和学段的关系
				iCommodityAreaDao.insert(commodity.getCommodityAreas());
			}
		}
		if (commodity.getCommodityUserType() != null) {// 如果用户类型的ID为空则设置ID
			for (int i = 0; i < commodity.getCommodityUserType().size(); i++) {
				if (StringUtils.isEmpty(commodity.getCommodityUserType().get(i).getId())) {
					commodity.getCommodityUserType().get(i).setId(IdCreater.getId());
					commodity.getCommodityUserType().get(i).setCommodityId(commodity.getId());
				}
			}
			// 添加和用户类型的关系
			if(commodity.getCommodityUserType().size() > 0) {
				commodityUserTypeDao.insert(commodity.getCommodityUserType());
			}
		}
		if (commodity.getCommodityEducational() != null) {// 如果学制的ID为空则设置ID
			List<String> ids = new ArrayList<String>();
			for (int i = 0; i < commodity.getCommodityEducational().size(); i++) {
				if (StringUtils.isEmpty(commodity.getCommodityEducational().get(i).getId())) {
					commodity.getCommodityEducational().get(i).setId(IdCreater.getId());
					commodity.getCommodityEducational().get(i).setCommodityId(commodity.getId());
				}
				ids.add(commodity.getCommodityEducational().get(i).getEducationalId());
				String systemNames = systemDao.findSystemByIds(ids);
				if (systemNames != null) {
					systemNames.replaceAll(",", " ");
				}
				commodity.setSystemName(systemNames);
			}
			commodityEducationalDao.insert(commodity.getCommodityEducational());// 添加和学制的关系
		}
		List<String> childIds = new ArrayList<String>();
		for (int i = 0; i < commodity.getCommodityRelationship().size(); i++) {
			if (StringUtils.isEmpty(commodity.getCommodityRelationship().get(i).getId())) {
				commodity.getCommodityRelationship().get(i).setParentId(commodity.getId());
				commodity.getCommodityRelationship().get(i).setId(IdCreater.getId());
			}
			childIds.add(commodity.getCommodityRelationship().get(i).getChildId());// 得到childId;
		}

		List<String> queryProductIds = commodityProductDao.queryProductIdsByCommodityIds(childIds);// 得到商品ids
		List<CommodityProductLine> commodityProductLines = new ArrayList<CommodityProductLine>();
		CommodityProductLine commodityProductLine = null;
		Set<String> productLineNameId = new HashSet<String>();// 通过产品ID得到产品线id 保证集合里的值都唯一
		for (String productId : queryProductIds) {
			Product product = productDao.selectProductById(productId);
			productLineNameId.add(product.getProductLine());
		}
		for (String string : productLineNameId) {// 将产品线的值设置到实体里
			commodityProductLine = new CommodityProductLine();
			commodityProductLine.setId(IdCreater.getId());
			commodityProductLine.setProductLineNameId(string);
			commodityProductLine.setCommodityId(commodity.getId());
			commodityProductLines.add(commodityProductLine);
			System.out.println(commodityProductLine);
		}
		if (commodityProductLines.size() > 0) {// 保存商品和产品线的关系
			iCommodityProductLineDao.insert(commodityProductLines);
		}
		List<String> productLinesId = new ArrayList<String>();// 将set集合转成list集合
		productLinesId.addAll(productLineNameId);
		String productLineNames = null;
		if (productLinesId.size() > 0) {
			productLineNames = productLineDao.selectProductLineByListId(productLinesId);
		}
		if (!StringUtils.isEmpty(productLineNames)) {
			commodity.setProductLineName(productLineNames);
		}
		if(commodity.getCommodityRelationship().size() > 0) {
			commodityRelationshipDao.insert(commodity.getCommodityRelationship());// 组合商品
		}
		if (isInsert) {
			commondityDao.insert(commodity);
		} else {
			commodity.setModifyDate(new Timestamp(System.currentTimeMillis()));
			commondityDao.updateByCommodity(commodity);
		}
	}

	/**
	 * 产品同步成商品 或者编辑单品时调用此方法
	 */
	@Override
	public void synchronizeCommodityOrUpdate(Commodity commodity) {
		// TODO Auto-generated method stub
		if(commodity.getInsertOrUpdate() != null) {
			if(commodity.getInsertOrUpdate() == Constant.UPDATE) {
				CommodityProduct commodityProduct = new CommodityProduct();
				commodityProduct.setProductId(commodity.getCommodityProduct().getProductId());
				CommodityProduct findByCommodityProduct = commodityProductDao.findByCommodityProduct(commodityProduct);
				if(findByCommodityProduct == null) {
					return ;
				}
				commodity.setCommodityProduct(commodityProduct);
				commodity.setId(findByCommodityProduct.getCommodityId());
				commodity.setModifyDate(new Timestamp(System.currentTimeMillis()));
				Commodity com = new Commodity();
				com.setId(commodity.getId());
				List<Commodity> queryByCase = commondityDao.queryByCase(com);
				if(queryByCase.size() > 0) {
					commodity.setIsGive(queryByCase.get(0).getIsGive());
				}else {
					return ;
				}
			}
		}
		Boolean isInsert = true; // 判断增加或修改的标准
		if (StringUtils.isEmpty(commodity.getId())) {// 如果商品的id为空则设置ID
			commodity.setId(IdCreater.getId());
			commodity.setIsGive(Constant.COMMODITY_IS_GIVE_B);
			commodity.setIsSubmitAudit(Constant.COMMODITY_IS_SUBMITAUDIT_A);
			commodity.setIsSingleProduct(Constant.COMMODITY_IS_SINGLEPRODUCT_A);// 默认单品
			commodity.setIsGrounding(Constant.COMMODITY_IS_GROUP_A); // 默认未上架
			commodity.setIsHasSaledate(Constant.COMMODITY_IS_HAS_SALEDATE_B); // 是否有销售期
			commodity.setIsPrice(Constant.COMMODITY_IS_PRICE_A); // 默认未定价
			commodity.setModifyDate(new Timestamp(System.currentTimeMillis()));
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			commodity.setCreateDate(ts);  //创建日期
			ProductLine productLine = productLineDao.selectProductLineById(commodity.getCommodityProduct().getProductSource());
			String commodityCode = null;
			if(productLine != null) {//生成商品code
				int sy = iCodeCreate.createCode("ys");
				int sp = iCodeCreate.createCode("sp");
				commodityCode = productLine.getCode()+"YS"+sy+"SP"+sp;
			}
			commodity.setCommodityCode(commodityCode);
		} else {
			isInsert = false;
			deleteById(commodity.getId());
		}
		Boolean insertOrUpdate = false;//判断是不是同步修改
		if(commodity.getInsertOrUpdate() != null) {
			if(commodity.getInsertOrUpdate() == Constant.UPDATE) {
				insertOrUpdate = false;
			}else {
				insertOrUpdate = true;
			}
		}else {
			insertOrUpdate = true;
		}
		if(insertOrUpdate) {	//只要不是同步的更新都要执行这段语句
		//	if (commodity.getIsGive() == Constant.COMMODITY_IS_GIVE_B) { // 非赠品
				if (commodity.getIsSubmitAudit() == null
						|| commodity.getIsSubmitAudit() == Constant.COMMODITY_IS_SUBMITAUDIT_A) {// 未提交审核
					commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_A); // 审核状态  未提交审核
					commodity.setIsEdited(Constant.COMMODITY_IS_EDIT_A); // 未提交审核
				} else {// 编辑时， 提交审核 此时需要将数据保存到审核历史表中
					Auditrecord auditrecord = new Auditrecord();
					auditrecord.setId(IdCreater.getId());
					auditrecord.setAuditDate(new Timestamp(System.currentTimeMillis()));
					if(StringUtils.isEmpty(commodity.getId())) {
						auditrecord.setSponsor(commodity.getCreatorId());	
					}else {
						auditrecord.setSponsor(commodity.getModifyUser());	
					}
					auditrecord.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_B);
					auditrecord.setCommodityId(commodity.getId());
					auditrecordDao.insert(auditrecord);
					commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_B);
					commodity.setIsEdited(Constant.COMMODITY_IS_EDIT_B);
				}
			/* }else {
				if (commodity.getIsSubmitAudit() == null || commodity.getIsSubmitAudit() == -1) {// 未提交审核 -1:代表未提交审核 1： 代表提交审核 同步时肯定先走这个步骤
					commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_A); // 审核状态
					commodity.setIsSubmitAudit(Constant.COMMODITY_IS_SUBMITAUDIT_A);
					commodity.setIsEdited(Constant.COMMODITY_IS_EDIT_A); // 未提交审核
				} else {// 编辑时， 提交审核 此时需要将数据保存到审核历史表中
					Auditrecord auditrecord = new Auditrecord();
					auditrecord.setId(IdCreater.getId());
					auditrecord.setAuditDate(new Timestamp(System.currentTimeMillis()));
					if(StringUtils.isEmpty(commodity.getId())) {
						auditrecord.setSponsor(commodity.getCreatorId());
					}else {
						auditrecord.setSponsor(commodity.getModifyUser());
					}
					auditrecord.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_D); // 如果是赠品直接通过审核
					auditrecord.setCommodityId(commodity.getId());
					auditrecordDao.insert(auditrecord);
					commodity.setIsSubmitAudit(Constant.COMMODITY_IS_SUBMITAUDIT_B);
					commodity.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_D);
					commodity.setIsEdited(Constant.COMMODITY_IS_EDIT_B);
				}
			}*/
		}
		if (commodity.getCommoditySaledepartments() != null) {// 如果销售部门的ID为空则设置ID
			for (int i = 0; i < commodity.getCommoditySaledepartments().size(); i++) {
				if(StringUtils.isEmpty(commodity.getCommoditySaledepartments().get(i).getSaleDepartmentId())) {
					//为空移除集合
					commodity.getCommoditySaledepartments().remove(i);
					continue;
				}
				if (StringUtils.isEmpty(commodity.getCommoditySaledepartments().get(i).getId())) {
					commodity.getCommoditySaledepartments().get(i).setId(IdCreater.getId());
					commodity.getCommoditySaledepartments().get(i).setCommodityId(commodity.getId());
				}
			}
			if(commodity.getCommoditySaledepartments().size() > 0) {
				commoditySaledepartmentDao.insert(commodity.getCommoditySaledepartments());// 增加和销售部门的关系
			}
		}
		if (commodity.getCommodityLevels() != null) {// 如果学段的ID为空则设置ID
			for (int i = 0; i < commodity.getCommodityLevels().size(); i++) {

				if(StringUtils.isEmpty(commodity.getCommodityLevels().get(i).getLevelId())) {
					//为空移除集合
					commodity.getCommodityLevels().remove(i);
					continue;
				}

				if (StringUtils.isEmpty(commodity.getCommodityLevels().get(i).getId())) {
					commodity.getCommodityLevels().get(i).setId(IdCreater.getId());
					commodity.getCommodityLevels().get(i).setCommodityId(commodity.getId());
				}
			}
			if(commodity.getCommodityLevels().size() > 0) {// 添加和学段的关系
				commodityLevelDao.insert(commodity.getCommodityLevels());
			}
		}

		if (commodity.getCommodityAreas() != null) {// 保存和地区的关系
			for (int i = 0; i < commodity.getCommodityAreas().size(); i++) {

				if(StringUtils.isEmpty(commodity.getCommodityAreas().get(i).getAreaId())) {
					//为空移除集合
					commodity.getCommodityAreas().remove(i);
					continue;
				}

				if (StringUtils.isEmpty(commodity.getCommodityAreas().get(i).getId())) {
					commodity.getCommodityAreas().get(i).setId(IdCreater.getId());
					commodity.getCommodityAreas().get(i).setCommodityId(commodity.getId());
				}
			}
			if(commodity.getCommodityAreas().size() > 0) {
				iCommodityAreaDao.insert(commodity.getCommodityAreas());
			}
		}
		if (commodity.getCommoditySpecialty() != null) {// 如果学科的ID为空则设置ID
			List<String> ids = new ArrayList<String>();
			for (int i = 0; i < commodity.getCommoditySpecialty().size(); i++) {

				if(StringUtils.isEmpty(commodity.getCommoditySpecialty().get(i).getSpecialtyId())) {
					//为空移除集合
					commodity.getCommoditySpecialty().remove(i);
					continue;
				}

				if (StringUtils.isEmpty(commodity.getCommoditySpecialty().get(i).getId())) {
					commodity.getCommoditySpecialty().get(i).setId(IdCreater.getId());
					commodity.getCommoditySpecialty().get(i).setCommodityId(commodity.getId());
				}
				ids.add(commodity.getCommoditySpecialty().get(i).getSpecialtyId());
			}
			String specialtyNames = iSpecialtyDao.findSpecialtyByIds(ids);// 学科名
			if (specialtyNames != null) {
				specialtyNames.replaceAll(",", " ");
			}
			commodity.setSpecialtyName(specialtyNames);
			if(commodity.getCommoditySpecialty().size() > 0) {// 添加和学科的关系
				commoditySpecialtyDao.insert(commodity.getCommoditySpecialty());
			}
		}
		if (commodity.getCommodityGrade() != null) {// 如果年级的ID为空则设置ID
			List<String> ids = new ArrayList<String>();
			for (int i = 0; i < commodity.getCommodityGrade().size(); i++) {

				if(StringUtils.isEmpty(commodity.getCommodityGrade().get(i).getGradeId())) {
					//为空移除集合
					commodity.getCommodityGrade().remove(i);
					continue;
				}

				if (StringUtils.isEmpty(commodity.getCommodityGrade().get(i).getId())) {
					commodity.getCommodityGrade().get(i).setId(IdCreater.getId());
					commodity.getCommodityGrade().get(i).setCommodityId(commodity.getId());
				}
				ids.add(commodity.getCommodityGrade().get(i).getGradeId());
			}
			String gradeNames = iGradeDao.findGradeByIds(ids);	// 年级名
			if (gradeNames != null) {
				gradeNames.replaceAll(",", " ");
			}
			commodity.setGradeName(gradeNames);
			if(commodity.getCommodityGrade().size() > 0) {	// 添加和年级的关系
				commodityGradeDao.insert(commodity.getCommodityGrade());
			}
		}
		if (commodity.getCommodityUserType() != null) {	// 如果用户类型的ID为空则设置ID
			for (int i = 0; i < commodity.getCommodityUserType().size(); i++) {

				if(StringUtils.isEmpty(commodity.getCommodityUserType().get(i).getUserTypeId())) {
					//为空移除集合
					commodity.getCommodityUserType().remove(i);
					continue;
				}

				if (StringUtils.isEmpty(commodity.getCommodityUserType().get(i).getId())) {
					commodity.getCommodityUserType().get(i).setId(IdCreater.getId());
					commodity.getCommodityUserType().get(i).setCommodityId(commodity.getId());
				}
			}
			if(commodity.getCommodityUserType().size() > 0) {// 添加和用户类型的关系
				commodityUserTypeDao.insert(commodity.getCommodityUserType());
			}
		}
		if (commodity.getCommodityEducational() != null) {	// 如果学制的ID为空则设置ID
			List<String> ids = new ArrayList<String>();
			for (int i = 0; i < commodity.getCommodityEducational().size(); i++) {

				if(StringUtils.isEmpty(commodity.getCommodityEducational().get(i).getEducationalId())) {
					//为空移除集合
					commodity.getCommodityEducational().remove(i);
					continue;
				}

				if (StringUtils.isEmpty(commodity.getCommodityEducational().get(i).getId())) {
					commodity.getCommodityEducational().get(i).setId(IdCreater.getId());
					commodity.getCommodityEducational().get(i).setCommodityId(commodity.getId());
				}
				ids.add(commodity.getCommodityEducational().get(i).getEducationalId());
				String systemNames = systemDao.findSystemByIds(ids);
				if (systemNames != null) {
					systemNames.replaceAll(",", " ");
				}
				commodity.setSystemName(systemNames);// 设置学制名
			}
			if(commodity.getCommodityEducational().size() > 0) {// 添加和学制的关系
				commodityEducationalDao.insert(commodity.getCommodityEducational());
			}
		}
		List<CommodityProductLine> commodityProductLines = new ArrayList<CommodityProductLine>();
		CommodityProductLine commodityProductLine = new CommodityProductLine();
		Product product = productDao.selectProductById(commodity.getCommodityProduct().getProductId());
		commodityProductLine.setProductLineNameId(product.getProductLine());
		commodityProductLine.setId(IdCreater.getId());
		commodityProductLine.setCommodityId(commodity.getId());
		commodityProductLines.add(commodityProductLine);
		if(commodityProductLines.size() > 0) {
			iCommodityProductLineDao.insert(commodityProductLines);// 保存商品和产品线的关系
		}
		ProductLine productLine = productLineDao.selectProductLineById(product.getProductLine());
		if (productLine != null) {// 读取产品线的名字
			commodity.setProductLineName(productLine.getName());
		}
		if (StringUtils.isEmpty(commodity.getCommodityProduct().getId())) {// 如果新增商品和产品的关系生成ID
			commodity.getCommodityProduct().setId(IdCreater.getId());
			commodity.getCommodityProduct().setCommodityId(commodity.getId());
		}
		commodityProductDao.insert(commodity.getCommodityProduct());// 保存产品的特有属性, 将商品和产品关联 单品
		if (isInsert) {// 保存商品的特有属性
			commondityDao.insert(commodity);
		} else {
			commodity.setModifyDate(new Timestamp(System.currentTimeMillis()));
			commondityDao.updateByCommodity(commodity);
		}
	}
	/**
	 * 提审
	 */
	@Override
	public List<String> commitAudit(List<String> ids, String sponsor) {
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(ids)) {
			return ids;
		}
		List<String> idsAudit = new ArrayList<String>(); // 存放可以提交审核的id
		List<String> idsThrough = new ArrayList<String>(); // 存放直接提审通过的id
		List<String> commodityNames = new ArrayList<String>();  //存放不能提交审核的商品名
		Commodity commodity = null;
		for (String id : ids) {
			commodity = new Commodity();
			commodity.setId(id);
			List<Commodity> queryByCase = commondityDao.queryByCase(commodity);
			if(queryByCase.size() < 1) {
				continue;
			}
			commodity = queryByCase.get(0);
			List<String> commodityIds = new ArrayList<String>();
			commodityIds.add(Constant.COMMODITY_REVIEW_STATUS_A);
			commodityIds.add(Constant.COMMODITY_REVIEW_STATUS_E);
			commodityIds.add(Constant.COMMODITY_REVIEW_STATUS_F);
			if (StringUtils.isEmpty(commodity.getDiscount())) {
				commodityNames.add(commodity.getCommodityName());
				continue;
			}
			if (commodity.getIsSubmitAudit() == Constant.COMMODITY_IS_SUBMITAUDIT_A
					&& commodityIds.contains(commodity.getAuditStatus())) {		// 判断能否提交审核
				Auditrecord auditrecord = new Auditrecord();
				auditrecord.setId(IdCreater.getId());
				auditrecord.setSponsor(sponsor);
				if (Constant.COMMODITY_IS_SINGLEPRODUCT_A.equals(commodity.getIsSingleProduct())) {	// 判断是否为单品
					/*if (commodity.getIsGive() == Constant.COMMODITY_IS_GIVE_A) {	// 判断是否可赠送 如果可赠送 直接审核通过
						auditrecord.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_D); // 如果是赠品直接通过审核
						idsThrough.add(id);
					} else {*/
						auditrecord.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_B); // 提交审核但是一审还未同意
						idsAudit.add(id);
					/*}*/
				} else {
					if (commodity.getDiscount() == 1) {// 未打折 直接审核通过
						auditrecord.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_D);
						idsThrough.add(id);
					} else {
						auditrecord.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_B);
						idsAudit.add(id);
					}
				}
				auditrecord.setAuditDate(new Timestamp(System.currentTimeMillis()));
				auditrecord.setCommodityId(id);
				auditrecordDao.insert(auditrecord);// 保存到审核历史表中
			}else {
				commodityNames.add(commodity.getCommodityName());  //存放不能提交审核的商品
			}
		}
		Commodity commodityAudit = new Commodity();
		commodityAudit.setIsSubmitAudit(Constant.COMMODITY_IS_SUBMITAUDIT_B);
		commodityAudit.setIsEdited(Constant.COMMODITY_IS_EDIT_B);
		if (idsAudit.size() > 0) {	// 提交审核，没有直接审核通过
			commodityAudit.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_B);
			commodityAudit.setIds(idsAudit);
			commondityDao.updateByCommodity(commodityAudit);
		}
		if (idsThrough.size() > 0) {		// 直接审核通过
			commodityAudit.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_D);
			commodityAudit.setIds(idsThrough);
			commondityDao.updateByCommodity(commodityAudit);
		}

		return commodityNames;
	}

	/**
	 * 上架
	 */
	@Override
	public void group(Commodity commodity) {
		// TODO Auto-generated method stub
		Commodity commodity1 = new Commodity();
		commodity1.setId(commodity.getId());
		commodity1 = commondityDao.queryByCase(commodity1).get(0);
		if (Constant.COMMODITY_REVIEW_STATUS_D.equals(commodity1.getAuditStatus())) {
			commondityDao.updateByCommodity(commodity);
		}
	}

	public Commodity findById(String id) {
		Commodity commodity = new Commodity();
		commodity.setId(id);
		List<Commodity> queryByCase = commondityDao.queryByCase(commodity);
		if (queryByCase.size() == 0) {
			return null;
		} else {
			commodity = queryByCase.get(0);
		}
		Commodity commodity2 = new Commodity();
		commodity2 = commodity;
		List<String> ids = new ArrayList<String>();
		ids.add(commodity.getId());
		if (Constant.COMMODITY_IS_SINGLEPRODUCT_A.equals(commodity.getIsSingleProduct())) {		// 如果是单品
			List<CommodityProduct> commitSingleCommodity = commondityDao.commitSingleCommodity(ids);
			commodity2.setCommodityProducts(commitSingleCommodity);
		} else {		// 如果是组合品
			List<CommodityProduct> findZH = commondityDao.findZHById(id); //通过ID查询组合品信息
			commodity2.setCommodityProducts(findZH);
		}
		commodity2.setCommodityLevels(commodityLevelDao.findByCommodity(id));
		commodity2.setCommodityEducational(commodityEducationalDao.findByCommodity(id));
		commodity2.setCommodityGrade(commodityGradeDao.findByCommodity(id));
		commodity2.setCommoditySpecialty(commoditySpecialtyDao.findByCommodity(id));
		commodity2.setCommodityUserType(commodityUserTypeDao.findByCommodity(id));
		commodity2.setCommodityAreas(iCommodityAreaDao.findByCommodity(id));
		commodity2.setCommoditySaledepartments(commoditySaledepartmentDao.findByCommodity(id));
		return commodity2;
	}

	@Override
	public Commodity findCommodityDetail(String id) {
		// TODO Auto-generated method stub
		Commodity commodity = new Commodity();
		commodity.setId(id);
		List<Commodity> queryByCase = commondityDao.queryByCase(commodity);
		if (queryByCase.size() == 0) {
			return null;
		} else {
			commodity = queryByCase.get(0);
		}
		Commodity commodity2 = new Commodity();
		commodity2 = commodity;
		List<String> ids = new ArrayList<String>();
		ids.add(commodity.getId());
		if (Constant.COMMODITY_IS_SINGLEPRODUCT_A.equals(commodity.getIsSingleProduct())) {
			// 如果是单品
			List<CommodityProduct> commitSingleCommodity = commondityDao.commitSingleCommodity(ids);
			commodity2.setCommodityProducts(commitSingleCommodity);
		} else {
			// 如果是组合品
			/**
			 * 通过ID查询组合品信息
			 */
			List<CommodityProduct> findZH = commondityDao.findZHById(id);
			commodity2.setCommodityProducts(findZH);
		}
		commodity2.setCommodityLevels(commodityLevelDao.findByCommodity(id));
		commodity2.setCommodityEducational(commodityEducationalDao.findByCommodity(id));
		commodity2.setCommodityGrade(commodityGradeDao.findByCommodity(id));
		commodity2.setCommoditySpecialty(commoditySpecialtyDao.findByCommodity(id));
		commodity2.setCommodityUserType(commodityUserTypeDao.findByCommodity(id));
		commodity2.setCommodityAreas(iCommodityAreaDao.findByCommodity(id));
		commodity2.setCommoditySaledepartments(commoditySaledepartmentDao.findByCommodity(id));
		//查询销售日期的列表
		Saledate saledate = new Saledate();
		saledate.setCommodityId(id);
		commodity2.setSaledates(iSaledateDao.findSaledateByCase(saledate));
		saledate = new Saledate();
		saledate.setCommodityId(id);
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		saledate.setCurrentDate(ts);
		List<Saledate> findSaledateByCase = iSaledateDao.findSaledateByCase(saledate);
		if(findSaledateByCase.size()>0) {
			commodity2.setSaledate(iSaledateDao.queryBySaleId(findSaledateByCase.get(0).getId()));
		}
		commodity2.setAuditrecords(auditrecordDao.findInfoByCommodityId(id));
		return commodity2;
	}

	@Override
	public void copy(List<String> ids) {
		// TODO Auto-generated method stub
		Commodity commodity = null;
		for (String id : ids) {
			commodity = new Commodity();
			commodity.setId(id);
			List<Commodity> commodities = commondityDao.queryByCase(commodity);
			if(commodities == null) {
				continue;
			}else {
				if(commodities.size() == 0) {
					continue;
				}
			}
			Commodity commodity2 = commodities.get(0);
			// 将商品部分数据进行初始化
			commodity2.setId(IdCreater.getId());
			commodity2.setDiscount(null);
			commodity2.setCurrentPrice(null);
			commodity2.setOriginalPrice(null);
			commodity2.setAuditStatus(Constant.COMMODITY_REVIEW_STATUS_A);
			commodity2.setIsGive(Constant.COMMODITY_IS_GIVE_B);
			commodity2.setIsEdited(Constant.COMMODITY_IS_EDIT_A);
			commodity2.setIsPrice(Constant.COMMODITY_IS_PRICE_A);
			commodity2.setIsHasSaledate(Constant.COMMODITY_IS_HAS_SALEDATE_B);
			commodity2.setIsSubmitAudit(Constant.COMMODITY_IS_SUBMITAUDIT_A);
			commodity2.setIsGrounding(Constant.COMMODITY_IS_GROUP_A);
			if (Constant.COMMODITY_IS_SINGLEPRODUCT_A.equals(commodity2.getIsSingleProduct())) {// 如果是单品
 				List<CommodityProduct> commodityProducts = commodityProductDao.findByCommodityId(id);
				if(commodityProducts.size() < 1) {
					continue;
				}
				CommodityProduct commodityProduct = null;
				for (int i = 0; i < commodityProducts.size(); i++) {
					commodityProduct = new CommodityProduct();
					commodityProduct.setId(IdCreater.getId());
					commodityProduct.setCommodityId(commodity2.getId());
					commodityProduct.setProductId(commodityProducts.get(i).getProductId());
					commodityProduct.setProductSource(commodityProducts.get(i).getProductSource());
					commodityProductDao.insert(commodityProduct);
				}
				System.out.println(commodityProduct.getProductSource());
				ProductLine productLine = productLineDao.selectProductLineById(commodityProducts.get(0).getProductSource());
				String commodityCode = null;
				if(productLine != null) {//生成商品code
					int sy = iCodeCreate.createCode("ys");
					int sp = iCodeCreate.createCode("sp");
					commodityCode = productLine.getCode()+"YS"+sy+"SP"+sp;
				}
				commodity2.setCommodityCode(commodityCode);
			} else {// 组合品
				CommodityRelationship commodityRelationship = new CommodityRelationship();
				commodityRelationship.setParentId(id);
				List<CommodityRelationship> commodityRelationships = commodityRelationshipDao
						.queryByCase(commodityRelationship);
				for (int i = 0; i < commodityRelationships.size(); i++) {
					commodityRelationships.get(i).setId(IdCreater.getId());
					commodityRelationships.get(i).setParentId(commodity2.getId());
				}
				String commodityCode = null;
				//生成编码
				Set<String> set = new HashSet<String>();
				if(commodityRelationships != null) {
					String productLineId = null;
					for (CommodityRelationship commodityRelationship1 : commodityRelationships) {
						String childId = commodityRelationship1.getChildId();
						List<CommodityProductLine> findByCommodityId = iCommodityProductLineDao.findByCommodityId(childId); //单品只有一个产品线
						if(findByCommodityId.size() > 0) {
							set.add(findByCommodityId.get(0).getProductLineNameId());
							productLineId = findByCommodityId.get(0).getProductLineNameId();
						}
					}
					if(set.size() == 1) {//同一产品线
						ProductLine productLine = productLineDao.selectProductLineById(productLineId);
						if(productLine != null) {//生成商品code
							int zh = iCodeCreate.createCode("zh");
							int sp = iCodeCreate.createCode("sp");
							commodityCode = productLine.getCode()+"ZH"+zh+"sp"+sp;
						}
					}else {//不同产品线
						int zh = iCodeCreate.createCode("zh");
						int sp = iCodeCreate.createCode("sp");
						int pl = iCodeCreate.createCode("pl");
						commodityCode = "D"+pl+"ZH"+zh+"SP"+sp;
					}
				}
				commodity2.setCommodityCode(commodityCode);
				commodityRelationshipDao.insert(commodityRelationships);
			}
			// 复制学制， 学科， 用户类型， 年级， 学段等数据
			List<CommodityEducational> commodityEducationals = commodityEducationalDao.findByCommodity(id);
			if (commodityEducationals.size() != 0) {
				for (int i = 0; i < commodityEducationals.size(); i++) {
					commodityEducationals.get(i).setCommodityId(commodity2.getId());
					commodityEducationals.get(i).setId(IdCreater.getId());
				}
				if(commodityEducationals.size() > 0) {
					commodityEducationalDao.insert(commodityEducationals);
				}
			}
			List<CommodityGrade> commodityGrades = commodityGradeDao.findByCommodity(id);
			if (commodityGrades.size() != 0) {
				for (int i = 0; i < commodityGrades.size(); i++) {
					commodityGrades.get(i).setCommodityId(commodity2.getId());
					commodityGrades.get(i).setId(IdCreater.getId());
				}
				if(commodityGrades.size() > 0) {
					commodityGradeDao.insert(commodityGrades);
				}
			}
			List<CommodityLevel> commodityLevels = commodityLevelDao.findByCommodity(id);
			if (commodityLevels.size() != 0) {
				for (int i = 0; i < commodityLevels.size(); i++) {
					commodityLevels.get(i).setCommodityId(commodity2.getId());
					commodityLevels.get(i).setId(IdCreater.getId());
				}
				if(commodityLevels.size() > 0) {
					commodityLevelDao.insert(commodityLevels);
				}
			}
			List<CommoditySpecialty> commoditySpecialties = commoditySpecialtyDao.findByCommodity(id);
			if (commoditySpecialties.size() != 0) {
				for (int i = 0; i < commoditySpecialties.size(); i++) {
					commoditySpecialties.get(i).setCommodityId(commodity2.getId());
					commoditySpecialties.get(i).setId(IdCreater.getId());
				}
				if(commoditySpecialties.size() > 0) {
					commoditySpecialtyDao.insert(commoditySpecialties);
				}
			}
			List<CommodityUserType> commodityUserTypes = commodityUserTypeDao.findByCommodity(id);
			if (commodityUserTypes.size() != 0) {
				for (int i = 0; i < commodityUserTypes.size(); i++) {
					commodityUserTypes.get(i).setCommodityId(commodity2.getId());
					commodityUserTypes.get(i).setId(IdCreater.getId());
				}
				if(commodityUserTypes.size() > 0) {
					commodityUserTypeDao.insert(commodityUserTypes);
				}
			}

			List<CommodityProductLine> commodityProductLines = iCommodityProductLineDao.findByCommodityId(id);
			if (commodityProductLines.size() != 0) {
				for (int i = 0; i < commodityProductLines.size(); i++) {
					commodityProductLines.get(i).setCommodityId(commodity2.getId());
					commodityProductLines.get(i).setId(IdCreater.getId());
				}
				if(commodityProductLines.size() > 0) {
					iCommodityProductLineDao.insert(commodityProductLines);
				}
			}
			List<CommoditySaledepartment> commoditySaledepartments = commoditySaledepartmentDao.findByCommodity(id);
			if (commoditySaledepartments.size() != 0) {
				for (int i = 0; i < commoditySaledepartments.size(); i++) {
					commoditySaledepartments.get(i).setCommodityId(commodity2.getId());
					commoditySaledepartments.get(i).setId(IdCreater.getId());
				}
				if(commoditySaledepartments.size() > 0) {
					commoditySaledepartmentDao.insert(commoditySaledepartments);
				}
			}
			List<CommodityArea> commodityAreas = iCommodityAreaDao.findByCommodity(id);
			if(commodityAreas.size() != 0) {
				for (int i = 0; i < commodityAreas.size(); i++) {
					commodityAreas.get(i).setCommodityId(commodity2.getId());
					commodityAreas.get(i).setId(IdCreater.getId());
				}
				if(commodityAreas.size() > 0) {
					iCommodityAreaDao.insert(commodityAreas);
				}
			}
			commondityDao.insert(commodity2);
		}
	}

	@Override
	public List<CommodityProduct> commitSingleCommodity(List<String> commodityIds) {
		// TODO Auto-generated method stub
		return commondityDao.commitSingleCommodity(commodityIds);
	}

	/**
	 * 修改时先删除和商品关联的数据
	 */
	@Override
	public void deleteById(String id) {
		// TODO Auto-generated method stub
		boolean flag = true;	// 默认可删除
		Commodity commodity = new Commodity();
		commodity.setId(id);
		commodity = commondityDao.queryByCase(commodity).get(0);
		if (Constant.COMMODITY_IS_SINGLEPRODUCT_A.equals(commodity.getIsSingleProduct())) {// 判断该单品是否被打包过
			List<CommodityRelationship> queryByChildId = commodityRelationshipDao.queryByChildId(id);
			if (queryByChildId.size() > 0) {
				flag = false;	// 不做删除 打包过
			} else {		// 删除单品和产品的对应关系
				commodityProductDao.delete(id);
			}
		} else {		// 组合品删除商品和商品之间的关系
			commodityRelationshipDao.delete(id);
		}
		if (flag) {
			// 删除商品和学段 学制 年级 学科 用户类型 销售部门 地区的对应关系
			commodityGradeDao.delete(id);
			commodityEducationalDao.delete(id);
			commodityLevelDao.delete(id);
			commoditySaledepartmentDao.delete(id);
			commodityUserTypeDao.delete(id);
			commoditySpecialtyDao.delete(id);
			iCommodityProductLineDao.delete(id);
			iCommodityAreaDao.delete(id);
		}
	}

	/**
	 * 生成组合商品的编码
	 * @return
	 */
	public String createZHCode() {

		return null;
	}
}
